/*
 *
 *  This file is part of Cfu
 *
 *  Cfu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Cfu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Cfu. If not, see <http://www.gnu.org/licenses/>.
 *
 *  cfuapplication.cc - william le ferrand (william@corefarm.com) 29/04/2010 15:30
 *
 */


// STL
#include <stdio.h>
#include <iostream>
#include <exception>

// QT
#include <QMenu>
#include <QMessageBox>
#include <QDebug>
#include <QApplication>

// CFU
#include <errors.hpp>
#include <configuration.hpp>
#include <submitter.hpp>

#include <cfuapplication.hpp>
#include <mainwindow.hpp>
#include <aboutdialog.hpp>
#include <parametersdialog.hpp>

cfuapplication_t::cfuapplication_t(configuration_t *conf, submitter_t *subm) :
  QObject(), 
  _newJobAction(new QAction(tr("Launch a rendering job"), this)),
  _submitJobAction(new QAction (tr("Submit a rendering job"), this)),
  _parametersAction(new QAction(tr("Set parameters"), this)),
  _aboutAction(new QAction(tr("About"), this)), 
  _quitAction(new QAction(tr("Quit"), this)),
  _trayIcon(new QSystemTrayIcon(this)), 
  _trayIconNormal(":/icons/logo"), 
  _conf(conf), 
  _subm(subm)

{
  std::cout << "@@@ Building cfuapplication_t object\n"; 
  
  _newJobAction->setIcon(QIcon(":/icons/start"));
  
  _mainWindow = new mainwindow_t(_submitJobAction);
  _subm->setMainWindow (_mainWindow); 
  connect(_trayIcon, 
	  SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
	  this, 
	  SLOT(_onTrayActivated(QSystemTrayIcon::ActivationReason)));


  _newJobAction->setIcon(QIcon(":/icons/start")); 
  connect(_newJobAction, SIGNAL(triggered()), this, SLOT(_onLaunchJob()));

  connect(_submitJobAction, SIGNAL(triggered()), this, SLOT(_onSubmitJob()));
  
  _parametersAction->setIcon(QIcon(":/icons/start")); 
  connect(_parametersAction, SIGNAL(triggered()), this, SLOT(_onParameters()));
  
  _aboutAction->setIcon(QIcon(":/icons/window")); 
  connect(_aboutAction, SIGNAL(triggered()), this, SLOT(_onAbout()));

  _quitAction->setIcon(QIcon(":/icons/close"));
  connect(_quitAction, SIGNAL(triggered()), this, SLOT(_onQuit()));
  
  _trayIcon->setContextMenu(new QMenu(_mainWindow));

  _trayIcon->contextMenu()->clear();

  _trayIcon->contextMenu()->addAction(_newJobAction);
  _trayIcon->contextMenu()->addAction(_parametersAction);
  _trayIcon->contextMenu()->addAction(_aboutAction);
  _trayIcon->contextMenu()->addAction(_quitAction);
  
  _trayIcon->setIcon(_trayIconNormal);
  _trayIcon->setToolTip(tr("Corefarm.com Uploader"));
  _trayIcon->show();
    
};

cfuapplication_t::~cfuapplication_t() {}; 

void cfuapplication_t::_onTrayActivated(QSystemTrayIcon::ActivationReason reason)
{
  if(reason == QSystemTrayIcon::Trigger)
    {
      if(_mainWindow->isVisible())
	_onHideWindow();
      else
	_onShowWindow();
    } ;
};

void cfuapplication_t::_onShowWindow()
{
  _mainWindow->show();
}

void cfuapplication_t::_onHideWindow()
{
  _mainWindow->hide();
}

void cfuapplication_t::_onQuit()
{
  QApplication::exit(0);
}

void cfuapplication_t::_onLaunchJob()
{
  std::cout << "@@@ On launch job signal caught\n"; 
  if(! _mainWindow->isVisible())
    {
      _onShowWindow();
    }  
}

void cfuapplication_t::_onAbout()
{
  std::cout << "@@@ About action\n"; 
  aboutdialog_t dialog (NULL) ; 
  dialog.exec () ;
  
  return; 
}

void cfuapplication_t::_onParameters()
{
  parametersdialog_t dialog (NULL, _conf); 
  dialog.exec (); 
  return; 
}


void cfuapplication_t::_onSubmitJob () 
{
  std::cout << "@@@ On submitJob\n"; 
  
  QString file = _mainWindow->getFile (); 
  int ghz = _mainWindow->getGhz () ; 
  int is_exr = _mainWindow->getIsExr (); 

  _mainWindow->setEnabled (false); 

  QString login = _conf->getLogin () ; 

  if (!login.length ())
    {
      parametersdialog_t dialog (NULL, _conf); 
      dialog.exec (); 
    }
  
  try 
    {
      _mainWindow->updateStatus ("Packing files and uploading to the server, please be patient ..."); 
      _subm->submit(file, ghz, is_exr); 
      _mainWindow->updateStatus ("Job submitted! Check your manager at http://www.corefarm.com/manager to get its completion status. You'll receive an email when done");
      _mainWindow->setEnabled (true) ;
    } 
  
  catch (missing_file &e) 
    {
      _mainWindow->updateStatus (QLatin1String("There is a missing file on your system: ") + QLatin1String(e.what ()) + QLatin1String (". Please correct this and submit again.")) ; 
      _mainWindow->setEnabled (true) ;
    }

   catch (corefarm_error &e) 
    {
      _mainWindow->updateStatus (QLatin1String(e.what ()) + QLatin1String (".")) ; 
      _mainWindow->setEnabled (true) ;
    }
  
  catch (std::exception &e) 
    {
      _mainWindow->updateStatus (QLatin1String("Something went wrong locally: ") + QLatin1String(e.what ())) ; 
      _mainWindow->setEnabled (true) ;
    }
  catch (...)
    {
      _mainWindow->updateStatus (QLatin1String("An unknown error has occurred. Please contact the support on www.corefarm.com")) ; 
      _mainWindow->setEnabled (true); 
    } 
  
  return ; 
}
